package com.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.domain.Issue;
import com.domain.IssueComment;
import com.domainVO.CommentUserIsMeVO;
import com.domainVO.CommentUserVO;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mapper.helping.IssueMapper;
import com.qiniu.common.QiniuException;
import com.service.IIssueCommentService;
import com.util.UserUtil;
import com.verify.UserVerify;
import com.verify.WeiXinVerify;
import com.websocketOfNotice.msgAnno.Notice;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import living.com.result.ApiResult;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author 大忽悠
 * @create 2022/2/10 13:35
 */
@Api(tags = "评论请求处理")
@RestController
@RequiredArgsConstructor
public class CommentController {
    private final IIssueCommentService iIssueCommentService;
    private final ObjectMapper objectMapper;
    private final IssueMapper issueMapper;

    @Notice(id_index = 0,notice_type = "COMMENT_ON_ISSUE")
    @UserVerify
    @WeiXinVerify(paramIndex = 1,paramName = "String",weiXinVerifyType = 0,needImgCheck = true,needVideoCheck = true)
    @ApiOperation("在指定问题下进行评论,返回评论id")
    @PostMapping("/comment")
    @Transactional(value = "primaryTransactionManager")
    public ApiResult<Integer> commentOnTargetIssue(@ApiParam("问题id") @RequestParam Integer issue_id,
                                                   @ApiParam("评论内容")@RequestParam String content,
                                                   @ApiParam("评论图片")@RequestParam(required = false) String pics,
                                                   @ApiParam("评论视频")@RequestParam(required = false) String videos,
                                         @ApiParam("是否匿名")@RequestParam(defaultValue = "0")Integer is_anony) throws JsonProcessingException {
        Assert.isTrue(content.length()<=1000,"评论字数最多1000字");
        //问题需要存在
        Issue issue = issueMapper.selectById(issue_id);
        Assert.isTrue(issue!=null&&issue.getIsDeleted()==0,"问题不存在");
        //回答可以带上图片和视频
        IssueComment comment = IssueComment.builder()
                .isAnony(is_anony)
                .issueId(issue_id)
                .content(content)
                .userId(UserUtil.getUserId())
                .build();
        //判断图片数量是否超过五张
        if(pics!=null&&pics!="")
        {
            //格式正确与否
            if(!(pics.startsWith("[")&&pics.endsWith("]")))
            {
                throw new IllegalArgumentException("路径格式错误");
            }
            List list = objectMapper.readValue(pics, List.class);
            Assert.isTrue(list.size()<=5,"图片最多五张");
            comment.setImgs(pics);
        }
        //视频最多一个
        if(videos!=null&&videos!="")
        {
            //格式正确与否
            if(!(videos.startsWith("[")&&videos.endsWith("]")))
            {
                throw new IllegalArgumentException("路径格式错误");
            }
            List list = objectMapper.readValue(videos, List.class);
            Assert.isTrue(list.size()<=1,"视频最多一个");
            comment.setVideos(videos);
        }
        iIssueCommentService.save(comment);
        //问题的回复数加一
        issue.setCommentSum(issue.getCommentSum()+1);
        issueMapper.updateById(issue);
        return ApiResult.success(comment.getId());
    }

    @GetMapping("/comments")
    @ApiOperation("返回指定问题下所有评论")
   public ApiResult<Map<String,Object>>  getCommentsUnderTargetIssue(@ApiParam("问题id")@RequestParam Integer issue_id,
                                                                     @ApiParam("起始页")@RequestParam Integer startPage,
                                                                     @ApiParam("一页多少条数据")@RequestParam Integer pageSize)
   {
       Map<String, Object> res = iIssueCommentService.getCommentsUnderTargetIssue(issue_id, startPage, pageSize);
       return ApiResult.success(res);
   }

   @ApiOperation("获取指定评论的详细信息")
   @GetMapping("/comment")
   public ApiResult<CommentUserIsMeVO> getTargetCommentInfoDetail(@ApiParam("评论id")@RequestParam Integer comment_id) throws JsonProcessingException {
       CommentUserIsMeVO res=iIssueCommentService.getTargetCommentInfoDetail(comment_id);
       return ApiResult.success(res);
   }

    @GetMapping("/AllComments")
    @ApiOperation("获取我发布的所有评论")
   public ApiResult<Map<String,Object>> getMyComments(@ApiParam("起始页")@RequestParam Integer startPage,
                                                                      @ApiParam("一页多少条数据")@RequestParam Integer pageSize) throws JsonProcessingException {
       Map<String, Object> res = iIssueCommentService.getMyComments(startPage, pageSize);
       return ApiResult.success(res);
   }

  // @UserVerify
   //@ApiOperation("删除指定评论")
  // @DeleteMapping("/comment")
   public ApiResult<Boolean> delTargetComment(@ApiParam("评论id")@RequestParam Integer comment_id) throws QiniuException, JsonProcessingException {
      Boolean res=iIssueCommentService.delTargetComment(comment_id);
       return ApiResult.success(res);
   }

    @UserVerify
    @ApiOperation("逻辑删除指定评论")
    @DeleteMapping("/logicDelComment")
    public ApiResult<Boolean> logicDelTargetComment(@ApiParam("问题id")@RequestParam Integer issue_id,@ApiParam("评论id")@RequestParam Integer comment_id) throws QiniuException {
        Boolean res=iIssueCommentService.logicDelTargetComment(comment_id);
        return ApiResult.success(res);
    }
}
